package com.airlines.crewassignment.controller.dict;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.airlines.base.entity.dict.Select2;
import com.airlines.common.Select2Util;
import com.airlines.crewassignment.constant.BaseConstant;
import com.airlines.crewassignment.constant.CrewConstant;
import com.airlines.crewassignment.domain.Crew;
import com.airlines.crewassignment.domain.LinkLine;
import com.airlines.crewassignment.entity.basicinfo.Code;
import com.airlines.crewassignment.service.basicinfo.CodeService;
import com.airlines.crewassignment.service.crew.CrewService;
import com.airlines.crewassignment.service.dict.Select2Service;
import com.airlines.crewassignment.service.linkline.LinkLineService;

/**
 * @author xxxx
 * @date 2018/5/21
 * @company xxxx Airlines
 * @description
 */
@RequestMapping("select2")
@Controller
public class Select2Controller {
	@Autowired
	private CrewService crewService;
	@Autowired
	private CodeService codeService;
	@Autowired
	private Select2Service select2Service;
	@Autowired
	private LinkLineService linkLineService;

	/**
	 * 机组人员下拉框
	 * 
	 * @param moduleFlag
	 *            如果为空，则查询所有
	 * @return
	 */
	@RequestMapping("getCrew")
	@ResponseBody
	public String getCrew(String moduleFlag) {
		List<Crew> list = crewService.listCrewBase(moduleFlag);
		return Select2Util.toJosn(list, "workcode", "cName");
	}

	/**
	 * 机组人员下拉框
	 * 
	 * @param moduleFlag
	 *            如果为空，则查询所有
	 * @return
	 */
	@RequestMapping("getCrewPCode")
	@ResponseBody
	public String getCrewPCode(String moduleFlag) {
		List<Crew> list = crewService.listCrewBase(moduleFlag);
		return Select2Util.toJosn(list, "pCode", "cName");
	}

	/**
	 * 人员下拉框：远程加载用 key 如果为空，则查询为空, 姓名和拼音模糊查询 moduleFlag 人员类型
	 * 
	 * @return
	 */
	@RequestMapping("getStaff")
	@ResponseBody
	public String getStaff(String key, String moduleFlag, String defaultVal) {
		Map<String, String> parameters = new HashMap<String, String>();
		parameters.put("key", key);
		parameters.put("moduleFlag", moduleFlag);
		parameters.put("workcode", defaultVal);
		List<Crew> list = crewService.listStaff(parameters);
		return Select2Util.toJosn(list, "workcode", "cName");
	}

	/**
	 * 人员下拉框：远程加载用 key 如果为空，则查询为空, 姓名和拼音模糊查询 moduleFlag 人员类型
	 * 
	 * @return
	 */
	@RequestMapping("getStaffPCode")
	@ResponseBody
	public String getStaffPCode(String key, String moduleFlag, String defaultVal) {
		Map<String, String> parameters = new HashMap<String, String>();
		parameters.put("key", key);
		parameters.put("moduleFlag", moduleFlag);
		parameters.put("pCode", defaultVal);
		List<Crew> list = crewService.listStaffPcode(parameters);
		return Select2Util.toJosn(list, "pCode", "cName");
	}

	/**
	 * 机场下拉框
	 * 
	 * @return
	 */
	@RequestMapping("getAirport")
	@ResponseBody
	public List<Select2> getAirport() {
		List<Select2> list = select2Service.getAirportCityCnName();
		return (list);
	}

	/**
	 * code类型下拉框 根据moduleFlag+parentCode确定要选的下拉框内容 moduleFlag F C A N:所有人可用
	 * parentCode G1加机组 D1 不在位
	 * 
	 * @return
	 */
	@RequestMapping("getCodeType")
	@ResponseBody
	public String getCodeType(String moduleFlag, String parentCode) {
		Code code = new Code();
		code.setParentCode(parentCode);
		List<Code> list = new ArrayList<Code>();
		// moduleFlag 不为空则要组装本角色可用以及通用的code
		if (StringUtils.isNotEmpty(moduleFlag)) {
			code.setModuleFlag(moduleFlag);
			List<Code> list1 = codeService.list(code);
			list.addAll(list1);
			code.setModuleFlag(CrewConstant.DUTY_N);
			List<Code> list2 = codeService.list(code);
			list.addAll(list2);
		} else {
			list = codeService.list(code);
		}
		return Select2Util.toJosn(list, "cCode", "cName");
	}

	/**
	 * 根据航班日期获取该日期下的连线
	 * 
	 * @param flightDate
	 * @return
	 */
	@RequestMapping("getCrewLinkLine")
	@ResponseBody
	public String getCrewLinkLine(Date flightDate) {
		List<LinkLine> crewLinkLineList = linkLineService.listLinkLineBase(CrewConstant.DUTY_F, flightDate);
		if (!CollectionUtils.isEmpty(crewLinkLineList)) {
			for (LinkLine crewLinkLine : crewLinkLineList) {
				crewLinkLine.setFlightVoyage(
						crewLinkLine.getFlightCom() + BaseConstant.BLANK + crewLinkLine.getFlightVoyage());
			}
		}
		return Select2Util.toJosn(crewLinkLineList, "crewLinkLineNo", "flightVoyage");
	}

	@RequestMapping("getTechNoZ")
	@ResponseBody
	public List<Select2> getTechNoZ(String techType) {
		List<Select2> list = select2Service.getTechNoZ(techType);
		return (list);
	}

	@RequestMapping("getYandN")
	@ResponseBody
	public List<Select2> getYandN() {
		List<Select2> list = select2Service.getYandN();
		return (list);
	}

	/**
	 * 获取组搭配标准
	 * 
	 * @param moduleFlag
	 * @return
	 */
	@RequestMapping("getCrewNumberType")
	@ResponseBody
	public List<Select2> getCrewNumberType(String moduleFlag) {
		List<Select2> list = select2Service.getCrewNumberType(moduleFlag);
		return (list);
	}

	/**
	 * 获取机上岗位
	 * 
	 * @param rankFlag
	 * @return
	 */
	@RequestMapping("getFlightPosition")
	@ResponseBody
	public List<Select2> getFlightPosition(String rankFlag) {
		List<Select2> list = select2Service.getFlightPosition(rankFlag);
		return (list);
	}

	/**
	 * 获取任务类型
	 * 
	 * @param rankFlag
	 * @return
	 */
	@RequestMapping("getTaskType")
	@ResponseBody
	public List<Select2> getTaskType(String rankFlag) {
		List<Select2> list = select2Service.getFlightPosition(rankFlag);
		return (list);
	}

	@RequestMapping("getSelectDictItem")
	@ResponseBody
	public List<Select2> getSelect2(String dictionaryCode) {
		List<Select2> list = select2Service.getSelect2(dictionaryCode);
		return (list);
	}

}
